<?php
namespace Admin\Controller;
use Think\Controller;
use  Think\Upload;
class ArticleController extends CommonController{
	//显示首页面
	public function index(){
		$model=D('article');
		$info=$model->index();
		$this->assign('page',$info['show']);
		$this->assign('cate',$info['cate']);
		$this->assign('article',$info['article']);
		$this->display();
	}

	//添加页面的显示
	public function add(){
		$cates=M('category')->where(array('lock'=>1))->select();
		$info=class_one_array($cates);
		$this->assign('cate',$info);
		$this->display();
	}

	//添加数据的处理
	public function addDo(){
		if(!IS_POST){
			$this->error('数据异常提交');
			exit;
		}else{
			$model=D('article');
			if(!$model->create()){
				$this->error($model->getError());
				exit;
			}else{
				$info=$model->addDo();
				if($info){
					//dump($info);
					$this->success('添加成功',U('Article/index'));
				}else{
					$this->error('添加失败');
				}
			}
		}
	}
	//显示编辑界面
	public function edit(){
		$model=D('article');
		$info=$model->edit();
		$this->assign('category',$info['category']);
		$this->assign('cont',$info['cont']);
		$this->display();
	}
	//处理编辑的数据
	public function editDo(){
    $id=I('post.id');
		$model=D('article');
		if(!IS_POST){
			$this->error('非法提交');
			exit;
		}else{
			if(!$model->create()){
				$this->error($model->getError());
			}else{
		        $info=$model->editDo();
		        if($info==true){
		        	$this->success('编辑成功',U('Article/index'));
		        }else{
		        	//$this->error('编辑失败');
		        }
			    
		}
	}
}

   //回收站列表
   public function back(){
   	    $model=D('article');
		$info=$model->back();
		$this->assign('page',$info['show']);
		$this->assign('cate',$info['cate']);
		$this->assign('article',$info['article']);
		$this->display();
   }

   //撤销
   public function backdel(){
   	    if(IS_AJAX){
   	    	$model=D('article');
   	    	$info=$model->backdel();
   	    	if($info==true){
   	    		echo 1;
   	    	}else{
   	    		echo 0;
   	    	}
   	    }else{
   	    	$model=D('article');
   	    	$info=$model->backdel();
   	    	if($info==true){
   	    		$this->redirect('Article/back');
   	    	}else{
   	    		$this->error('撤销失败');
   	    	}
   	    }

   }
   //删除到回收站
   public function del(){
   	if(IS_AJAX){
   		$id=I('id');
   		$ids=explode(',',$id);
   		foreach($ids as $v){
   			$data['id']=$v;
   			$data['del']=0;
   			M('article')->where('id='.$data['id'])->save($data);
   		}
   		echo 1;
   	}else{
   		$data['id']=I('id');
   		$data['del']=0;
   		$info=M('article')->where('id='.$data['id'])->save($data);
   		$this->redirect('Article/index');
   	}
   }

   function img(){
        $upload=new Upload();
        $upload->rootPath='./Uploads/';
        $file=$upload->upload();
        if(!$file){
            //echo $this->ajaxReturn(['code'=>1]);
        }else{
            //dump($file);//js插件自带的myfilename
            $imgUrl=__ROOT__."/Uploads/".$file['myFileName']['savepath'].$file['myFileName']['savename'];
            echo   $imgUrl;
        }
    }
  //删除功能
  public  function  backdell(){
  	if(IS_AJAX){
  		$model=D('article');
  		$info=$model->backdell();
  		if($info==true){
  			echo 1;
  		}else{
  			echo 0;
  		}
  	}else{
  		$model=D('article');
  		$info=$model->backdell();
  		if($info==true){
  			$this->redirect('Article/back');
  		}else{
  			$this->error('删除失败');
  		}
  	}
  }
  
}